<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseItemTypeBase extends Base {
	var $table = '';
	var $prefix = '';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $format = true;
	var $format_check = false;
	var $format_prefix = 'field_';
	var $item_link = false;
	var $item_link_owner = false;
	var $item_fields = array('id', 'name', 'link');
	var $list_fields = array();
	var $search_fields = array('name');
	var $search_type = 'or';
	var $sort_order = true;
	var $sort_fields = array('sort_order+', 'name+');
	var $filter_fields = array();
	var $filter_map = array();
	var $filter_query = array();
	
	//
	var $type = '';
	var $types = array();
	var $type_limit = true;
	
	//constructor
	function BaseItemTypeBase($table = '') {
		parent::Base();
		
		if( $table != '' ) {
			$this->table = $table;
		}
	}
	
	function setType($type) {
		$this->type = $type;
	}
	
	function getType() {
		return $this->type;
	}
	
	function setProperty($name, $value = '') {
		if( is_array($name) ) {
			foreach($name as $k => $v) {
				$this->$k = $v;
			}
		} else {
			$this->$name = $value;
		}
	}
	
	function getProperty($name) {
		if( isset($this->$name) ) {
			return $this->$name;
		}
		
		return NULL;
	}
	
	function property($data) {
		if( is_array($data) ) {
			return $this->setProperty($data);
		} else {
			return $this->getProperty($data);
		}
	}
	
	function _pk() {
		return $this->prefix . $this->pkey;
	}
	
	function _pf($field) {
		return $this->prefix . $field;	
	}
	
	function setFilterQuery($name, $value = '') {
		if( is_array($name) ) {
			$this->filter_query = $name;
		} else {
			$this->filter_query[$name] = $value;
		}
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true) {
		$query_str = "";
		$query_str .= $this->_query_type();
		$query_str .= $this->_get_query_str($filter, $check);
		
		$query_order_str = "";
		if( $this->sort_order == true ) {
			if( is_array($this->sort_fields) ) {
				$query_order_str = " order by ";
				$i = 0;
				foreach($this->sort_fields as $k => $v) {
					if( substr($v, 0, 1) == '=' ) {
						$v = substr($v, 1);
					} else {
						$v = $this->prefix . $v;
					}
					if( substr($v, -1) == '-' ) {
						$as = 'desc';
						$v = substr($v, 0, -1);
					} elseif( substr($v, -1) == '+' ) {
						$as = 'asc';
						$v = substr($v, 0, -1);
					} else {
						$as = 'asc';
					}
					if( $i > 0 ) {
						$query_order_str .= ", ";
					}
					$query_order_str .= " " . $v . " " . $as;
					$i++;
				}
			} else {
				$query_order_str = " order by " . $this->prefix . "sort_order asc";
			}
		}
		
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. $query_str . $query_order_str . $query_limit_str;
		$rows = $this->adb->getRows($query);
		
		if( $this->format == true ) {
			for($i=0, $n=sizeof($rows); $i<$n; $i++) {
				$rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
			}
		}
		
		return $rows;
	}
	
	function getItemsTotal($filter = '', $check = 0) {
		$query_str = "";
		$query_str .= $this->_query_type();
		$query_str .= $this->_get_query_str($filter, $check);
		
		return $this->adb->getCountA($this->table, " " . $this->_query_owner_rt() . " " . $query_str . " ");
	}
	
	function getItemsFormat($filter = '', $check = 0, $limit = true, $field = '') {
		$items = $this->getItems($filter, $check, $limit);
		$pf = $this->prefix;
		if( $this->format == true ) {
			$pf = '';
		}
		$itemsf = array();
		for($i=0, $n=sizeof($items); $i<$n; $i++) {
			$f1 = $pf . 'id';
			$f2 = $pf . 'id';
			$f3 = 0;
			if( is_array($field) && sizeof($field) >= 2 ) {
				$f1 = $field['0'];
				$f2 = $field['1'];
				if( substr($f1, 0, 1) == '=' ) {
					$f1 = substr($f1, 1);
				} elseif( substr($f1, 0, 1) == '_' ) {
					$f1 = $pf . substr($f1, 1);
				} else {
					$f1 = $pf . $f1;
				}
				if( substr($f2, 0, 1) == '=' ) {
					$f2 = substr($f2, 1);
				} elseif( substr($f2, 0, 1) == '_' ) {
					$f2 = $pf . substr($f2, 1);
				} else {
					$f2 = $pf . $f2;
				}
			} elseif( $field != '' ) {
				$f2 = $field;
				if( substr($f2, 0, 1) == '=' ) {
					$f2 = substr($f2, 1);
				} elseif( substr($f2, 0, 1) == '_' ) {
					$f2 = $pf . substr($f2, 1);
				} else {
					$f2 = $pf . $f2;
				}
			} elseif( isset($items[$i][$pf . 'name']) && strlen($items[$i][$pf . 'name']) > 0 ) {
				$f2 = $pf . 'name';
			} elseif( isset($items[$i][$pf . 'subject']) && strlen($items[$i][$pf . 'subject']) > 0 ) {
				$f2 = $pf . 'subject';
			}
			$itemsf[$items[$i][$f1]] = $items[$i][$f2];
		}
		
		return $itemsf;
	}
	
	//
	function checkItem($value, $type = 'id') {
		$value = func_db_input($value);
		
		if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 && in_array($type, $this->item_fields) ) {
			$field = $this->prefix . $type;
		} elseif( in_array($type, array('id', 'link')) ) {
			$field = $this->prefix . $type;
			if( $type == 'id' ) {
				$value = intval($value);
			}
		} else {
			return false;
		}
		$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. " and " . $field . " = '" . $value . "'";
		
		return $this->adb->checkCount($query);
	}
	
	function getItem($value, $type = 'id') {
		$value = func_db_input($value);
		
		if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 && in_array($type, $this->item_fields) ) {
			$field = $this->prefix . $type;
		} elseif( in_array($type, array('id', 'link')) ) {
			$field = $this->prefix . $type;
			if( $type == 'id' ) {
				$value = intval($value);
			}
		} else {
			return false;
		}
		$query_str = "";
		$query_str .= " and " . $field . " = '" . $value . "'";
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
		
		$item = $this->adb->getRow($query);
		if( $this->format == true ) {
			$item = $this->_post_field($item, '', $this->prefix, $this->format_check);
		}
		
		return $item;
	}
	
	function addItem($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'type'] = $this->_get_type();
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			$data_array[$p . $this->_table_owner_field] = $this->owner;
		}
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		//$items_id = $this->adb->insertA($this->table, $data_array);
		$items_id = $this->_insert($this->table, $data_array);
		
		return $items_id;
	}
	
	function addItems($data_array) {
		$items_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$items_ids[] = $this->addItem($v);
			}
		}
		
		return $items_ids;
	}
	
	function updateItem($items_id, $data_array) {
		if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		//return $this->adb->updateA($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
		return $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function updateItems($items_ids, $data_array) {
		if( is_array($items_ids) ) {
			if( sizeof($items_ids) > 30 ) {
				foreach($items_ids as $k => $items_id) {
					$this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
				}
			} else {
				$query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
				$this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")");
			}
		}
	}
	
	function deleteItem($items_id) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$this->adb->deleteA($this->table, " " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
		
		return true;
	}
	
	function deleteItems($items_ids) {
		if( is_array($items_ids) ) {
			$this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
		}
	}
	
	function setItemsStatus($items_ids, $status = 1) {
		if( is_array($items_ids) ) {
			$this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
		}
	}
	
	//
	function check($items_id) {
		$count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
		
		return ($count > 0) ? true : false;
	}
	
	function get($items_id) {
		return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function add($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		return $this->adb->insertA($this->table, $data_array);
	}
	
	function update($items_id, $data_array) {
		if( !$this->_not_null($items_id) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
		
		$data_array[$this->prefix . 'last_modified'] = 'now()';
		$data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	function delete($items_id) {
		return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
	}
	
	//private
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} elseif( $this->_table_status_usable == true ) {
			$query_str .= " and " . $this->prefix . "status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				if( is_array($this->search_fields) ) {
					if( $this->search_type != 'or' ) {
						$this->search_type = 'and';
					}
					$i = 0;
					$query_str .= " and ( ";
					foreach($this->search_fields as $k => $field) {
						if( $i > 0 ) {
							$query_str .= " " . $this->search_type . " ";
						}
						$query_str .= " " . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
						$i++;
					}
					$query_str .= " ) ";
				} elseif( is_string($this->search_fields) ) {
					$query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
				}
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and " . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and " . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
				}
			}
			if( isset($filter['category']) ) {
				if( is_array($filter['category']) ) {
					$query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
			if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
				foreach($filter as $k => $v) {
					if( in_array($k, $this->filter_fields) ) {
						if( is_array($v) && sizeof($v) > 0 ) {
							$query_str .= " and " . $this->prefix . $k . " in ('" . implode("','", $v) . "') ";
						} elseif( is_int($v) ) {
							$query_str .= " and " . $this->prefix . $k . " = '" . $v . "' ";
						} elseif( is_numeric($v) ) {
							$query_str .= " and " . $this->prefix . $k . " = '" . $v . "' ";
						} elseif( is_string($v) ) {
							$query_str .= " and " . $this->prefix . $k . " like '%" . $v . "%' ";
						} else {
							$query_str .= " and " . $this->prefix . $k . " like '%" . $v . "%' ";
						}
					}
				}
			}
		}
		//
		$query_str .= $this->_get_query_str_custom($filter, $check);
		
		return $query_str;
	}
	
	function _get_query_str_custom($filter = '', $check = 0) {
		return "";
	}
	
	//type
	function _query_type($p = '', $type = '') {
		$query = "";
		$types = $this->types;
		$type = ($type == '') ? $this->type : '';
		if( substr($type, 0, 1) == '!' ) {
			$type = substr($type, 1);
			$query .= " and (";
			$i = 0;
			foreach($types as $k => $v) {
				if( $v != $type ) {
					if( $i > 0 ) {
						$query .= " or ";
					}
					$query .= (($p == '') ? '' : $p . ".") . $this->prefix . "type = '" . $v . "' ";
					$i++;
				}
			}
			$query .= ") ";
			
			return $query;
		} elseif( strstr($type, '|') !== false ) {
			$query .= " and (";
			$i = 0;
			$ts = explode("|", $type);
			foreach($ts as $k => $v) {
				if( $i > 0 ) {
					$query .= " or ";
				}
				$query .= (($p == '') ? '' : $p . ".") . $this->prefix . "type = '" . $v . "' ";
				$i++;
			}
			$query .= ") ";
			
			return $query;
		} else {
			if( in_array($type, $types) ) {
				return " and " . (($p == '') ? '' : $p . ".") . $this->prefix . "type = '" . $type . "' ";
			}
		}
		
		return '';
	}
	
	function _get_type() {
		if( $this->type_limit == true && is_array($this->types) ) {
			if( in_array($this->type, $this->types) ) {
				return $this->type;
			} else {
				return '';
			}
		}
		
		return $this->type;
	}
}
//
?>